package com.ymatou.iapi.sellerproduct.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.GetSetTopProductsCall;
import com.ymatou.iapi.sellerproduct.service.SetTopProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_SetTopProduct {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	private static ProductBasicInfoWapper productBasicInfoWapper = new ProductBasicInfoWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品橱窗位设置");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new SetTopProductCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	//商品加入橱窗
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetProductinTop() {
		Logger.start(true, "商品加入橱窗");
		try{
			//1 remove all productInTop
			SellerProductCallService.removeAllProductInTop(sellerId);
			
			//2 add new product
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			//3 add into top list
			List<String> productIds = new ArrayList<String>();
			productIds.add(productId);
			JSONObject jsonObject = SellerProductCallService.addProductsIntoTopList(sellerId, productIds);
			
			//check
			//1) 报文
			Logger.verifyEquals(200,jsonObject.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals(true,isProductInTop(sellerId,productId),"检查橱窗查询");
			
			//2) SQL and Mongo
			//SQL
	        List<YmtProductsI> rm = ymtProductsIWapper.selectProductByProductId(productId);
	        Logger.verifyEquals(1,rm.get(0).getIsTop(),"数据库SQL-Ymt_Products 橱窗标记位");
			
	        //Old Mongo
	        if (YmtMongoDriverFactroy.isOldMongoCheck()){
	        	Map<String, Object> ret = productBasicInfoWapper.getByProductId(productId);
	        	Logger.verifyEquals("true",ret.get("IsTop").toString().toLowerCase(),"数据库Mongo-ProductBasicInfo 橱窗标记位");
	        }
	        
	        //new mongo
	        if (YmtMongoDriverFactroy.isNewMongoCheck()){
	        	Map<String,Object> ret = productDetailModelWapper.getProductId(productId);
		        Logger.verifyEquals("true",ret.get("IsTop").toString().toLowerCase(),"数据库Mongo-ProductDetailModel 橱窗标记位");
	        }
	        
	        if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
	        	NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
	        }
	        
	        
	        
	        
	        //4 remove from top list
	        JSONObject removeObject = SellerProductCallService.removeProductsFromTopList(sellerId, productIds);
	        
	        //check
	        //1) 报文
			Logger.verifyEquals(200,removeObject.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals(false,isProductInTop(sellerId,productId),"检查橱窗查询");
			
			//2) SQL and Mongo
	        rm = ymtProductsIWapper.selectProductByProductId(productId);
	        Logger.verifyEquals(0,rm.get(0).getIsTop(),"数据库SQL-Ymt_Products 橱窗标记位");
			
	       //Old Mongo
	        if (YmtMongoDriverFactroy.isOldMongoCheck()){
	        	Map<String, Object> ret = productBasicInfoWapper.getByProductId(productId);
		        Logger.verifyEquals("false",ret.get("IsTop").toString().toLowerCase(),"数据库Mongo-ProductBasicInfo 橱窗标记位");
	        }
	        
	        //new mongo
	        if (YmtMongoDriverFactroy.isNewMongoCheck()){
	        	Map<String,Object> ret = productDetailModelWapper.getProductId(productId);
		        Logger.verifyEquals("false",ret.get("IsTop").toString().toLowerCase(),"数据库Mongo-ProductDetailModel 橱窗标记位");
	        }
	        
	        if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
	        	NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
	        }
	        
	        
		}catch(Exception e){
			Logger.fail(e);
		}
		
		
		
	}
	
	//商品移出橱窗
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetProductOutTop() {
		Logger.start(true, "商品移出橱窗");
		Logger.comment("在[Tc_SetProductinTop]已测");
		
	}
	
	
	
	private static boolean isProductInTop(int sellerId, String productId) throws Exception{
		
		Map<String,Object> updateMap = new HashMap<String,Object>();
		updateMap.put("UserId", sellerId);
		GetSetTopProductsCall getSetTopProductsCall = new GetSetTopProductsCall();
		JSONObject toplistObj = getSetTopProductsCall.callAndGetReturnData(updateMap);
		JSONArray products = toplistObj.getJSONObject("Data").getJSONArray("Products");
		for(int i=0;i<products.length();i++){
			if (productId.equals(products.getJSONObject(i).getString("ProductId"))){
				return true;
			}
		}
		
		
		return false;
		
		
		
		
		
	}
	
	

	

	
	
	
	
	
	
}
